//
//  PaylistViewController.m
//  KiklinkApp
//
//  Created by LJC on 14-9-15.
//  Copyright (c) 2014年 Kiklink. All rights reserved.
//

#import "PaylistViewController.h"

#import "ImagesManager.h"
#import "UIColor+hex.h"
#import "UIView+loadWithNib.h"

#import "MJRefresh.h"
#import "PaylistCell.h"

#import "API.h"

NSString *const paylistTableCellIdentifier = @"PaylistCell";

@interface PaylistViewController ()
{
    //data
    NSMutableArray *paylistTableArray;
    NSInteger page;
    NSInteger totalPage;
}
@end

@implementation PaylistViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"所有订单";

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithHexString:AppColor_BackGround];
    
    
    //loading
    [self.activity startAnimating];
    self.refreshBtn.hidden = YES;
    [self.refreshBtn addTarget:self action:@selector(refresh) forControlEvents:UIControlEventTouchUpInside];
    
    
    //BundleImages
    ImagesManager *imagesManager = [ImagesManager sharedImagesManager];
    
    
    //leftNavItem
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setFrame:CGRectMake(0, 0, NavBarHeight/4, NavBarHeight/2)];
    [backButton setBackgroundImage:[imagesManager back] forState:UIControlStateNormal];
    [backButton setBackgroundImage:[imagesManager backTapped] forState:UIControlStateHighlighted];
    [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftNavBarItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftNavBarItem;
    
    
    //paylistTable
    self.paylistTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.paylistTable.backgroundColor = [UIColor colorWithHexString:AppColor_BackGround];
    self.paylistTable.delegate = self;
    self.paylistTable.dataSource = self;
    self.paylistTable.showsVerticalScrollIndicator = NO;
    [self.paylistTable registerClass:[UITableViewCell class] forCellReuseIdentifier:paylistTableCellIdentifier];
    [self setupRefresh];
    
    
    //data
    paylistTableArray = [[NSMutableArray alloc] init];
    page = 1;
    [self getData:1 Condition:@"first"];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - get data
//condition:first&header&footer
- (void)getData:(NSInteger)pageNum Condition:(NSString *)condition
{
    NSLog(@"page:%ld",(long)pageNum);
    //    [API courseSearchWithPageNumber:[NSNumber numberWithInteger:pageNum] Keyword:@"" SortStyle:@"" Distance:@0 Tags:@"" Callback:^(int stateCode, NSString *word, id content) {
    //
    //        if (stateCode == SUCCESS) {
    //            ProductPage *productPage = (ProductPage *)content;
    //            NSMutableArray *result = [[NSMutableArray alloc] initWithArray:productPage.result];
    //
    //            if ([condition isEqualToString:@"first"]) {
    //                totalPage = productPage.lastPageNumber;
    //                self.chosenTable.scrollEnabled = YES;
    //                page = 2;
    //                chosenTableArray = result;
    //            } else if ([condition isEqualToString:@"header"]) {
    //                page = 2;
    //                chosenTableArray = result;
    //            } else if ([condition isEqualToString:@"footer"]) {
    //                page ++;
    //                [chosenTableArray addObjectsFromArray:result];
    //            }
    //
    //            [self loadData:condition];
    //
    //        } else {
    //            if ([condition isEqualToString:@"first"]) {
    //                self.chosenTable.scrollEnabled = NO;
    //                [self.activity stopAnimating];
    //                self.refreshBtn.hidden = NO;
    //            } else if ([condition isEqualToString:@"header"]) {
    //                [self.chosenTable headerEndRefreshing];
    //            } else if ([condition isEqualToString:@"footer"]) {
    //                [self.chosenTable footerEndRefreshing];
    //            }
    //        }
    //    }];
}

- (void)loadData:(NSString *)condition
{
    [self.paylistTable reloadData];
    
    if ([condition isEqualToString:@"first"]) {
        [self.activity stopAnimating];
    } else if ([condition isEqualToString:@"header"]) {
        [self.paylistTable headerEndRefreshing];
    } else if ([condition isEqualToString:@"footer"]) {
        [self.paylistTable footerEndRefreshing];
    }
}

- (void)refresh
{
    self.refreshBtn.hidden = YES;
    [self.activity startAnimating];
    
    [self getData:1 Condition:@"first"];
}
#pragma mark - get data End

#pragma mark - back
- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - back End

#pragma tableView dataSource&delegate  &MJRefresh
- (void)setupRefresh
{
    [self.paylistTable addHeaderWithTarget:self action:@selector(headerRereshing)];
    [self.paylistTable addFooterWithTarget:self action:@selector(footerRereshing)];
    
    self.paylistTable.headerPullToRefreshText = @"下拉可以刷新";
    self.paylistTable.headerReleaseToRefreshText = @"松开马上刷新";
    self.paylistTable.headerRefreshingText = @"正在刷新...";
    self.paylistTable.footerPullToRefreshText = @"上拉加载更多";
    self.paylistTable.footerReleaseToRefreshText = @"松开马上加载";
    self.paylistTable.footerRefreshingText = @"正在加载...";
}
- (void)headerRereshing
{
    //data
    [self getData:1 Condition:@"header"];
}
- (void)footerRereshing
{
    //data
    if (page <= totalPage) {
        [self getData:page Condition:@"footer"];
    } else {
        [self.paylistTable footerEndRefreshing];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return paylistTableArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BOOL nibsRegistered = NO;
    if (!nibsRegistered) {
        UINib *nib = [UINib nibWithNibName:@"PaylistCell" bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:paylistTableCellIdentifier];
        nibsRegistered = YES;
    }
    PaylistCell *cell = (PaylistCell *)[tableView dequeueReusableCellWithIdentifier:paylistTableCellIdentifier];
//        [cell loadDataWithPaylistItem:[paylistTableArray objectAtIndex:indexPath.row]];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 132;
}
#pragma tableView dataSource&delegate End

@end
